package com.canal.admin.controller;

import com.canal.admin.exception.BuzException;
import com.canal.admin.response.Result;
import com.canal.admin.response.ResultUtil;
import com.canal.admin.service.ZcolChannelService;
import com.canal.admin.transfer.req.channel.ChannelAddReq;
import com.canal.admin.transfer.req.channel.ChannelCondition;
import com.canal.admin.transfer.req.channel.ChannelUpReq;
import com.canal.admin.transfer.resp.PageResp;
import com.canal.admin.transfer.resp.canal.ChannelListResp;
import com.canal.admin.transfer.resp.canal.ChannelResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 渠道管理
 * @author yangbo
 * @date 2020-06-23 14:43
 */
@RestController
public class ChennelController extends BaseController {

    @Autowired
    private ZcolChannelService zcolChannelService;

    /**
     * 渠道列表
     * @param condition
     * @param bindingResult
     * @return
     * @throws BuzException
     */
    @PostMapping(value = "/channel/list")
    @PreAuthorize("isAuthenticated()")
    public Result<PageResp<ChannelListResp>> list(@RequestBody @Validated ChannelCondition condition,
                                                  BindingResult bindingResult) throws BuzException {
        dealReqInvalidError(bindingResult);
        return ResultUtil.makeSuccess(zcolChannelService.channelList(condition));
    }

    /**
     * 新增渠道
     * @param addReq
     * @param bindingResult
     * @return
     * @throws BuzException
     */
    @PostMapping(value = "/channel/add")
    @PreAuthorize("isAuthenticated()")
    public Result add(@RequestBody @Validated ChannelAddReq addReq,
                      BindingResult bindingResult) throws BuzException {
        dealReqInvalidError(bindingResult);
        zcolChannelService.addChannel(addReq);
        return ResultUtil.makeSuccess("新增成功");
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @PostMapping(value = "/channel/del/{id}")
    @PreAuthorize("isAuthenticated()")
    public Result del(@PathVariable Integer id) throws BuzException {
        zcolChannelService.delChannel(id);
        return ResultUtil.makeSuccess("删除成功");
    }

    /**
     * 明细
     * @param id
     * @return
     */
    @GetMapping(value = "/channel/detail/{id}")
    @PreAuthorize("isAuthenticated()")
    public Result<ChannelResp> detail(@PathVariable Integer id) {
        return ResultUtil.makeSuccess(zcolChannelService.detail(id));
    }

    /**
     * 编辑
     * @param upReq
     * @param bindingResult
     * @return
     * @throws BuzException
     */
    @PostMapping(value = "/channel/update")
    @PreAuthorize("isAuthenticated()")
    public Result edit(@RequestBody @Validated ChannelUpReq upReq,
                       BindingResult bindingResult) throws BuzException {
        dealReqInvalidError(bindingResult);
        zcolChannelService.update(upReq);
        return ResultUtil.makeSuccess("修改成功");
    }

    /**
     * 重置渠道密码
     * @param id
     * @return
     * @throws BuzException
     */
    @PostMapping(value = "/channel/reset/pwd/{id}")
    @PreAuthorize("isAuthenticated()")
    public Result restPwd(@PathVariable Integer id) throws BuzException {
        zcolChannelService.resetPwd(id);
        return ResultUtil.makeSuccess("重置成功");
    }
}
